package com.situ.student.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.situ.student.controller.StudentController;
import com.situ.student.entity.Student;
import com.situ.student.util.SwingUtil;

public class AddStudentFrame extends JFrame{
	private StudentController studentController =  new StudentController();//学生的Controller
	
	private MainFrame mainFrame;
	
	public AddStudentFrame(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}
	
	public void init() {
		this.setSize(300, 350);
		this.setLocation(300, 50);

		// // 根据屏幕分辨率设置到屏幕的中心
		// Toolkit toolkit = Toolkit.getDefaultToolkit();
		// Dimension screenSize = toolkit.getScreenSize();
		// mainFrame.setLocation((screenSize.width - 300) / 2,
		// (screenSize.height - 350) / 2);
		SwingUtil.setLocationCenter(this);

		//只是销毁这个
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		JPanel mainPanel = (JPanel) this.getContentPane();
		BoxLayout boxLayout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
		mainPanel.setLayout(boxLayout);
		// 姓名
		JPanel namePanel = new JPanel();
		JLabel nameLable = new JLabel("姓名： ");
		namePanel.add(nameLable);
		JTextField nameText = new JTextField();
		nameText.setPreferredSize(new Dimension(120, 30));
		namePanel.add(nameText);
		// 年龄
		JPanel agePanel = new JPanel();
		JLabel ageLable = new JLabel("年龄： ");
		agePanel.add(ageLable);
		JTextField ageText = new JTextField();
		ageText.setPreferredSize(new Dimension(120, 30));
		agePanel.add(ageText);
		// 性别
		JPanel genderPanel = new JPanel();
		JLabel genderLable = new JLabel("性别： ");
		genderPanel.add(genderLable);
		JTextField genderText = new JTextField();
		genderText.setPreferredSize(new Dimension(120, 30));
		genderPanel.add(genderText);
		//班级
		JPanel banjiPanel = new JPanel();
		JLabel banjiLable = new JLabel("班级： ");
		banjiPanel.add(banjiLable);
		JTextField banjiText = new JTextField();
		banjiText.setPreferredSize(new Dimension(120, 30));
		banjiPanel.add(banjiText);
		//添加按钮
		JPanel savePanel = new JPanel();
		JButton saveButton = new JButton("保存");
		saveButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("AddStudentFrame.init()");
				String name = nameText.getText().trim();
				System.out.println(name);
				int age = Integer.parseInt(ageText.getText().trim());
				System.out.println(age);
				String gender = genderText.getText().trim();
				System.out.println(gender);
				String banji = banjiText.getText().trim();
				System.out.println(banji);
				
				boolean result = studentController.insert(name, age, gender, banji);
				if (result) {//添加成功，弹出成功的窗口，刷新表格
					//保存成功或失败弹出对话框
					JOptionPane.showMessageDialog(null, "保存成功");
					mainFrame.reloadTable();
				}else {
					JOptionPane.showMessageDialog(null, "保存失败");
				}
				//关闭窗口
				AddStudentFrame.this.dispose();
			}
		});
		savePanel.add(saveButton);

		mainPanel.add(namePanel);
		mainPanel.add(agePanel);
		mainPanel.add(genderPanel);
		mainPanel.add(banjiPanel);
		mainPanel.add(savePanel);

		this.setVisible(true);
	}
}
